﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.SystemManagement
{
    public partial class users : BasePage
    {
        IUserBLL userBLL = (IUserBLL)IocContainer.Instance.Resolve<IUserBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            //SystemOverTime();
            if (!Page.IsPostBack)
            {                            
                // 按钮权限控制
                //ControlAuthority(new List<WebControl> { btnAdd, btnUpdate, btnDelete,btnSearch }, 29);
                LoadData();
            }
        }

        private void LoadData()
        {
            try
            {
                StringBuilder strs = new StringBuilder();
                string strWhere = "";
                if (!string.IsNullOrEmpty(this.txtUserName.Text.Trim()))
                    strWhere += string.Format(" and tsu.UserName like '%{0}%'", this.txtUserName.Text.Trim());
                DataTable dt = userBLL.Query(strWhere);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dataRow in dt.Rows)
                    {                        
                        strs.AppendFormat("<tr rel='{0}'>", dataRow["UserId"]);
                        strs.AppendFormat("<td><a herf='javascript:void(0)' class='openWindow'  id='" + dataRow["UserId"] + "'>用户权限</a></td><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td style='width:0px;display:none'>{5}</td><td style='width:0px;display:none'>{6}</td></tr>", dataRow["UserName"], dataRow["NewName"], dataRow["TypeName"], dataRow["rolename"], dataRow["Email"], dataRow["UserType"], dataRow["BillNum"]);
                    }
                }
                litUserList.Text = strs.ToString();
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }            
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int Id = 0;
                int.TryParse(this.selectedID.Value.Trim(), out Id);                
                userBLL.Delete(Id);
                LoadData();
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }

        }
    }
}